package no.weather.weatherProxy.wsKlima;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import no.weather.WeatherStation;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.location.Location;

public class WeatherStationHandler extends DefaultHandler {
	private Integer amsl = null;
	private Integer fromDay = null;
	private Integer fromMonth = null;
	private Integer fromYear = null;
	private Boolean includeNotProvidingStations;
	private Boolean isInStationItem = false;
	private String lastLocalName = null;
	private Double lat = null;
	private Double lon = null;
	private String name = null;
	private Integer stationNr = null;
	private List<WeatherStation> stations = new LinkedList<WeatherStation>();
	private Integer toDay = null;
	private Integer toMonth = null;
	private Integer toYear = null;

	public WeatherStationHandler(Boolean includeNotProvidingStations) {
		super();
		this.includeNotProvidingStations = includeNotProvidingStations;
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		String valueS = new String(ch, start, length);

		if (valueS.equals("\n")) {
			;
		} else if (isInStationItem)
			if (lastLocalName.equals("amsl")) {
				amsl = new Integer(valueS);
			} else if (lastLocalName.equals("fromDay")) {
				fromDay = new Integer(valueS);
			} else if (lastLocalName.equals("fromMonth")) {
				fromMonth = new Integer(valueS);
			} else if (lastLocalName.equals("fromYear")) {
				fromYear = new Integer(valueS);
			} else if (lastLocalName.equals("latDec")) {
				lat = new Double(valueS);
			} else if (lastLocalName.equals("lonDec")) {
				lon = new Double(valueS);
			} else if (lastLocalName.equals("name")) {
				name = valueS;
			} else if (lastLocalName.equals("stnr")) {
				stationNr = new Integer(valueS);
			} else if (lastLocalName.equals("toDay")) {
				toDay = new Integer(valueS);
			} else if (lastLocalName.equals("toMonth")) {
				toMonth = new Integer(valueS);
			} else if (lastLocalName.equals("toYear")) {
				toYear = new Integer(valueS);
			}

	}

	@Override
	public void endDocument() throws SAXException {
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("item")) {
			isInStationItem = false;
			newStation();

		}

	}

	public List<WeatherStation> getAllStations() {
		return stations;
	}

	private void newStation() {
		// If providing toYear=toMonth=toDay=0
		if (!includeNotProvidingStations
				& (toYear != 0 || toMonth != 0 || toDay != 0))
			return;
		else if (!includeNotProvidingStations & fromYear == 0 & fromMonth == 0
				& fromDay == 0)
			return;
		else {
			WeatherStation station = new WeatherStation();

			station.setDeliverTo(new Date(toYear, toMonth, toDay));
			toDay = null;
			toMonth = null;
			toYear = null;

			station.setDeliverFrom(new Date(fromYear, fromMonth, fromDay));
			fromDay = null;
			fromMonth = null;
			fromYear = null;

			Location loc = new Location(WsKlimaProxy.PROVIDER);
			loc.setAltitude(amsl);
			loc.setLatitude(lat);
			loc.setLongitude(lon);
			station.setLocation(loc);
			lat = null;
			lon = null;
			amsl = null;

			station.setName(name);
			name = null;

			station.setId(stationNr.toString());
			stationNr = null;

			station.setProvider(WsKlimaProxy.PROVIDER);
			station.setTypesIsKnown(false);
			station.setRefressRate(3600.);

			stations.add(station);
		}
	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) {
		if (localName.equals("item")) {
			String type = attributes.getValue(0);
			if (type.equals("ns3:no_met_metdata_StationProperties")) {
				isInStationItem = true;
			}
		} else {
			lastLocalName = localName;
		}
	}
}
